# 多阶段构建：构建阶段
FROM node:20-alpine AS builder
WORKDIR /app

# 安装依赖
COPY package*.json ./
RUN npm install

# 复制源代码并构建
COPY . .
# 构建 H5 版本用于生产环境
RUN npm run build:h5

# 生产阶段：使用nginx提供静态文件
FROM nginx:alpine

# 复制构建产物到nginx目录
# uni-app H5 构建输出通常在 dist/build/h5 目录
COPY --from=builder /app/dist/build/h5 /usr/share/nginx/html

# 创建自定义 nginx 配置以支持 SPA 路由
RUN echo 'server { \
    listen 80; \
    server_name localhost; \
    root /usr/share/nginx/html; \
    index index.html; \
    \
    # 支持 SPA 路由 \
    location / { \
        try_files $uri $uri/ /index.html; \
    } \
    \
    # 静态资源缓存 \
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { \
        expires 1y; \
        add_header Cache-Control "public, immutable"; \
    } \
    \
    # 安全头 \
    add_header X-Frame-Options "SAMEORIGIN" always; \
    add_header X-Content-Type-Options "nosniff" always; \
    add_header X-XSS-Protection "1; mode=block" always; \
}' > /etc/nginx/conf.d/default.conf

# 暴露端口
EXPOSE 80

# 启动nginx
CMD ["nginx", "-g", "daemon off;"]